package com.abbas.CayTcore.presenter

import com.abbas.CayTcore.adapter.InfoAdapter
import com.abbas.CayTcore.adapter.NowProductAdapter
import com.abbas.CayTcore.base.BasePresenter
import com.abbas.CayTcore.model.WindingHomeUplevelModel
import com.abbas.CayTcore.ui.WindingHomeViewModel
import com.abbas.CayTcore.view.ViewContract

/**
 * 绕线主页
 */
class WindingHomeUplevelPresenter :
    BasePresenter<ViewContract.WindingHomeUplevelView, WindingHomeUplevelModel>() {

    var windingViewModel: WindingHomeViewModel = WindingHomeViewModel()
    var infoAdapter: InfoAdapter = InfoAdapter()
    var productAdapter = NowProductAdapter()

    override fun initModel(): WindingHomeUplevelModel {
        return WindingHomeUplevelModel()
    }

    override fun initData() {
        windingViewModel.infoList.apply {
            observe(fragmentViewLifecycleOwner) {
                infoAdapter.data = it
                infoAdapter.notifyDataSetChanged() //-_-||
            }
        }
        windingViewModel.nowProductList.apply {
            observe(fragmentViewLifecycleOwner) {

            }
        }
    }

    /**
     * 获取提示
     */
    fun getInfo() {
        if (windingViewModel.infoList.value.isNullOrEmpty()) {
            model.getInfo {
                onSuccess {
                    windingViewModel.infoList.value = it.data
                    view.get()?.onInfoGetten(infoAdapter)
                }
            }
        } else {
            view.get()?.onInfoGetten(infoAdapter)
        }

    }


    fun getSchedule() {
        if (windingViewModel.nowProductList.value.isNullOrEmpty()) {
            model.getSchedule(1, 1) {
                onSuccess {
                    windingViewModel.nowProductList.value = it.data
                    view.get()?.onScheduleGetten(productAdapter)
                }
            }
        } else {
            view.get()?.onScheduleGetten(productAdapter)
        }
    }

}